﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ORM.Areas.Admin.ViewModels.ManageSpam;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models;
using ORM.Business.Models.Caching;
using ORM.Business.Models.Entities;
using ORM.Common;
using ORM.Framework.Filters;
using ORM.Web.Models;

namespace ORM.Areas.Admin.Controllers
{
    [PermissionAuthorize(Permissions = KeyObject.Permission.ManageSpam)]
    public class ManageSpamController : AdminControllerBase
    {
        private IQuestionRepository _questionRepository;
        private IGuideRepository _guideRepository;
        private ICommentRepository _commentRepository;
        public ManageSpamController(IQuestionRepository questionRepository,IGuideRepository guideRepository,ICommentRepository commentRepository)
        {
            _questionRepository = questionRepository;
            _guideRepository = guideRepository;
            _commentRepository = commentRepository;
        }
        #region Manage Report Questions

        public ViewResult ManageReportedQuestion(int? minReportNumber)
        {
            if (minReportNumber == null || minReportNumber < 1)
                minReportNumber = AppConfigs.MinReportNumber;
            var vm = new ListReportedQuestionViewModel
            {
                ReportedQuestions = _questionRepository.GetAllReportedQuestion(minReportNumber.Value).Data,
                MinReportNumber = minReportNumber.Value
            };

            return View(vm);
        }

        [HttpPost]
        public ActionResult DeleteReportedQuestion(int id)
        {
            Feedback feedback = _questionRepository.DeleteReportedQuestion(id);
            if (feedback.Success)
            {
                SetMessage("Spam Question and attached Reports has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        [HttpPost]
        public ActionResult DeleteFalseQuestionReports(int id)
        {
            Feedback feedback = _questionRepository.DeleteFalseReports(id);
            if (feedback.Success)
            {
                SetMessage("Reports attached to chosen Question has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        public ActionResult QuestionReportDetails(int id)
        {
            IList<ReportedQuestion> questionReports = _questionRepository.GetReportedQuestionDetails(id).Data;
            return View(questionReports);           
        }
        #endregion

        #region Manage Report Guides

        public ViewResult ManageReportedGuide(int? minReportNumber)
        {
            if (minReportNumber == null || minReportNumber < 1)
                minReportNumber = AppConfigs.MinReportNumber;
            var vm = new ListReportedGuideViewModel
            {
                ReportedGuides = _guideRepository.GetAllReportedGuide(minReportNumber.Value).Data,
                MinReportNumber = minReportNumber.Value
            };

            return View(vm);
        }

        [HttpPost]
        public ActionResult DeleteReportedGuide(int id)
        {
            Feedback feedback = _guideRepository.DeleteReportedGuide(id);
            if (feedback.Success)
            {
                SetMessage("Spam Guide and attached Reports has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        [HttpPost]
        public ActionResult DeleteFalseGuideReports(int id)
        {
            Feedback feedback = _guideRepository.DeleteFalseReports(id);
            if (feedback.Success)
            {
                SetMessage("Reports attached to chosen Guide has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        public ActionResult GuideReportDetails(int id)
        {
            IList<ReportedGuide> guideReports = _guideRepository.GetReportedGuideDetails(id).Data;
            return View(guideReports);
        }
        #endregion

        #region Manage Report GuideComments

        public ViewResult ManageReportedComment(int? minReportNumber)
        {
            if (minReportNumber == null || minReportNumber < 1)
                minReportNumber = AppConfigs.MinReportNumber;
            var vm = new ListReportedGuideCommentViewModel
            {
                ReportedGuideComments = _commentRepository.GetAllReportedComment(minReportNumber.Value).Data,
                MinReportNumber = minReportNumber.Value
            };

            return View(vm);
        }

        [HttpPost]
        public ActionResult DeleteReportedComment(int id)
        {
            Feedback feedback = _commentRepository.DeleteReportedComment(id);
            if (feedback.Success)
            {
                SetMessage("Spam GuideComment and attached Reports has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        [HttpPost]
        public ActionResult DeleteFalseCommentReports(int id)
        {
            Feedback feedback = _commentRepository.DeleteFalseReports(id);
            if (feedback.Success)
            {
                SetMessage("Reports attached to chosen GuideComment has been deleted successfully", MessageType.Success);

            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }


            // Redirect to current page after deleting
            return Redirect(Request.UrlReferrer.AbsoluteUri);
        }

        public ActionResult CommentReportDetails(int id)
        {
            IList<ReportedGuideComment> commentReports = _commentRepository.GetReportedCommentDetails(id).Data;
            return View(commentReports);
        }
        #endregion
    }
}
